const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        shop_info: {
            account: '',
            name: '',
            region: '',
            address: '',
            human_name: '',
            human_phone: '',
            is_disable: ''
        },
        qrcode: '',
        member_count: 0,
        number_count: 0,
    },

    /**
     * 页面准备就绪
     */
    onReady: function() {

        // 验证登录
        app.verifyLogin();

        // 获取店铺信息
        app.execute(this.getInfo);
    },

    /**
     * 获取店铺信息
     */
    getInfo() {
        let page = app.getPage();
        wx.request({
            url: app.data.service_url + 'shop/get_info',
            header: {
                'Client-Token': app.data.client_token
            },
            success: function(response) {
                if (200 === response.statusCode) {
                    if (0 === response.data.errno) {
                        page.setData({
                            info: response.data.data.info,
                            qrcode: response.data.data.qrcode,
                            member_count: response.data.data.member_count,
                            number_count: response.data.data.number_count
                        });
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    },

    /**
     * 点击解除绑定
     */
    tapUnbound: function() {
        wx.showModal({
            title: '确定要取消绑定吗？',
            success: function(e) {
                if (true === e.confirm) {
                    wx.showToast({
                        title: '请等待...',
                        icon: 'loading',
                        mask: true,
                        duration: 50000
                    });
                    wx.request({
                        url: app.data.service_url + 'shop/unbound',
                        header: {
                            'Client-Token': app.data.client_token
                        },
                        success: function(response) {
                            if (200 === response.statusCode) {
                                wx.hideToast();
                                if (0 === response.data.errno) {
                                    wx.showToast({
                                        title: response.data.message,
                                        mask: true,
                                    });
                                    setTimeout(function() {
                                        wx.reLaunch({
                                            url: '/pages/base_mine/index/index',
                                        });
                                    }, 1500);
                                }else{
                                    wx.showToast({
                                        title: response.data.message,
                                        icon: 'none'
                                    });
                                };
                            } else {
                                wx.hideToast();
                                wx.showToast({
                                    title: '连接远程服务返回错误',
                                    icon: 'none'
                                });
                            };
                        },
                        fail: function() {
                            wx.hideToast();
                            wx.showToast({
                                title: '连接远程服务失败',
                                icon: 'none'
                            });
                        }
                    });
                };
            }
        });
    }
});